/**
 * Application name : Recipes App
 * Date				: March 2012
 */
package com.recipes.app;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.recipes.nosdcard.NoSD_RecipesList;

public class SplashScreen extends Activity {
	ProgressBar prgLoading;

	int progress = 0;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash_screen);

		prgLoading = (ProgressBar) findViewById(R.id.prgLoading);
		prgLoading.setProgress(progress);

		new Loading().execute();
	}

	/** this class is used to handle thread */
	public class Loading extends AsyncTask<Void, Void, Void> {

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub

		}

		@Override
		protected Void doInBackground(Void... arg0) {
			// TODO Auto-generated method stub
			while (progress < 100) {
				try {
					Thread.sleep(1000);
					progress += 30;
					prgLoading.setProgress(progress);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			// Dev_MountInfo dev = Dev_MountInfo.getInstance();
			// DevInfo info = dev.getExternalInfo();// Internal SD Card

			String interSd = RecipesUtil.INSDPATH;
			String extSd = RecipesUtil.EXSDPATH;

			String extDb = RecipesUtil.EXSDPATH + "/db_file";
			String interDb = RecipesUtil.INSDPATH + "/db_file";

			String extImage = RecipesUtil.EXSDPATH + "/images";
			String interImage = RecipesUtil.INSDPATH + "/db_images";

			File f = new File(extSd);

			if (f.exists()) {
				File ff = new File(extDb);
				File fff = new File(extImage);
				if (ff.exists() && fff.exists()) {
					RecipesUtil.copy(extDb, interDb);
					RecipesUtil.copy(extImage, interImage);
				}
			}

			if (new File(interImage).exists() && new File(interDb).exists()) {
				Intent i = new Intent(SplashScreen.this, CategoryList.class);
				startActivity(i);
			} else {
				Toast.makeText(SplashScreen.this, "No SD card", 1000).show();
				// Intent i = new Intent(SplashScreen.this,
				// NoSD_RecipesList.class);
				// startActivity(i);
				finish();
			}

		}
	}
}